#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    Organiser::incoming_dir = "test/test_dir/New";
    Organiser::instance()->init();

    vector<Rule> rules = Org::Organiser::instance()->rules;
    tblPopulateRules(rules);

    Rule *row;
    row = new Rule();

    QObject::connect(ui->btnRunRules, SIGNAL(clicked()), this, SLOT(runRules()));
    QObject::connect(ui->actionNew_Rule, SIGNAL(triggered()), this, SLOT(newRule()));
    QObject::connect(ui->btnIncomingDir, SIGNAL(clicked()), this, SLOT(btnIncomingDir_clicked()));
    QObject::connect(ui->tblRules,SIGNAL(doubleClicked(QModelIndex)), this, SLOT(tblRulesClicked_showRule(QModelIndex)));
    QObject::connect(ui->btnExecute, SIGNAL(clicked()), this, SLOT(btnExecute_clicked()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::btnExecute_clicked()
{
    Organiser::instance()->execute_matches();
}

void MainWindow::runRules()
{
    qDebug("starting rules");
    Organiser::instance()->run_rules();
    QMessageBox::information(this, "RUN", "finished running rules, please verify then hit run rules again wheeeeeeee... im stuck inside this app.. help me");

    vector<MatchedPath> matches = Organiser::instance()->rule_matches;

    for(vector<MatchedPath>::const_iterator i = matches.begin(); i!=matches.end(); ++i)
    {
        MatchedPath match = (MatchedPath)*i;
        Rule cur_rule = match.rule;
        bf::path path = match.path;
        string new_path = (match.to_path).string();

        string m_path = match.path.string();

        std::stringstream out;
        std::string s;
        out << cur_rule.id;
        s = out.str();

        QTableWidgetItem *ruleId = new QTableWidgetItem(QString(s.c_str()));
        QTableWidgetItem *ruleName = new QTableWidgetItem(QString((cur_rule.rule_name).c_str())); //ruleName->setFlags(Qt::ItemIsEnabled);

        string s_action = Vars::get_action(cur_rule.action);

        QTableWidgetItem *ruleOperation = new QTableWidgetItem(QString((s_action).c_str()));
        QTableWidgetItem *dirPath = new QTableWidgetItem(QString(m_path.c_str()));

        QTableWidgetItem *newDirPath = new QTableWidgetItem(QString(new_path.c_str()));

        int row = ui->tblMatches->rowCount();
        ui->tblMatches->insertRow(row);

        QTableWidgetItem* chkBoxItem = new QTableWidgetItem();
        chkBoxItem->setCheckState(Qt::Checked);

        ui->tblMatches->setItem(row, 0, chkBoxItem);
        ui->tblMatches->setItem(row, 1, ruleId);
        ui->tblMatches->setItem(row, 2, ruleName);
        ui->tblMatches->setItem(row, 3, ruleOperation);
        ui->tblMatches->setItem(row, 4, dirPath);
        ui->tblMatches->setItem(row, 5, newDirPath);
    }
}

void MainWindow::newRule()
{
    frmRule *ruleNew = new frmRule();
    ruleNew->show();
}

void MainWindow::btnIncomingDir_clicked()
{
    QString path = QFileDialog::getExistingDirectory (this, tr("Directory"), "/home/krone");
    if ( path.isNull() == false )
    {
        ui->txtIncomingDir->setText(path);
        Organiser::incoming_dir = path.toStdString();
    }
}

void MainWindow::tblRulesClicked_showRule(QModelIndex ind)
{
    vector<Rule> rules = Organiser::instance()->rules;
    Rule rule;

    int c = 0;
    for(vector<Rule>::const_iterator i = rules.begin(); i!=rules.end(); ++i)
    {        
         if(c==ind.row())
         {
            rule = (Rule)*i;
            frmRule *ruleNew = new frmRule(&rule);
            ruleNew->show();
            break;
         }
         c++;
    }
}

void MainWindow::tblPopulateRules(const vector<Rule> rules )
{
    for(vector<Rule>::const_iterator i = rules.begin(); i!=rules.end(); ++i)
    {
        Rule cur_rule = (Rule)*i;

        std::stringstream out;
        std::string s;
        out << cur_rule.id;
        s = out.str();

        QTableWidgetItem *ruleId = new QTableWidgetItem(QString(s.c_str()));
        QTableWidgetItem *ruleName = new QTableWidgetItem(QString((cur_rule.rule_name).c_str())); //ruleName->setFlags(Qt::ItemIsEnabled);

        string s_action = RuleIO::get_map_action_key(cur_rule.action);

        QTableWidgetItem *ruleOperation = new QTableWidgetItem(QString((s_action).c_str()));
        QTableWidgetItem *ruleToDir = new QTableWidgetItem(QString(((cur_rule.to_dir).string()).c_str()));

        int row = ui->tblRules->rowCount();
        ui->tblRules->insertRow(row);

        QTableWidgetItem* chkBoxItem = new QTableWidgetItem();

        chkBoxItem->setCheckState(Qt::Unchecked);
        if(cur_rule.enabled)
            chkBoxItem->setCheckState(Qt::Checked);


        ui->tblRules->setItem(row, 0, chkBoxItem);
        ui->tblRules->setItem(row, 1, ruleId);
        ui->tblRules->setItem(row, 2, ruleName);
        ui->tblRules->setItem(row, 3, ruleOperation);
        ui->tblRules->setItem(row, 4, ruleToDir);

    }
}
